// page/lottery/lottery.js
Page({
    data: {
        isHide: false,
        showTips: false,
        animationData: {},
        awardsList: {},
        awardResult: "",
        runDegs: 0,
        awardsConfig: {  //奖项设置
            chance: true,
            awards: [
                { id: 0, img: "/images/draw3.png", title: "现金" },
                { id: 1, img: "/images/draw4.png", title: "优惠券20元" },
                { id: 2, img: "/images/draw5.png", title: "优惠券10元" },
                { id: 3, img: "/images/draw6.png", title: "店铺优惠券5元" },
                { id: 4, img: "/images/draw7.png", title: "小米手机一部" },
                { id: 5, img: "/images/draw8.png", title: "莫灰心" },
            ]
        }
    },

    onLoad: function (options) {
        var that = this
        that.setData({
            isHide: true
        })
    },
    onReady: function () {
        var that = this
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    width: res.windowWidth - 60
                })
            }
        })
    },
    onShow: function () {
        this.getto()
    },
    getto: function(){
        // 绘制转盘
        var awardsConfig = this.data.awardsConfig.awards,
        len = awardsConfig.length,
        rotateDeg = 360 / len / 2 + 90,
        html = [],
        turnNum = 1 / len  // 文字旋转 turn 值
        for (var i = 0; i < len; i++) {
            // 奖项列表
            html.push({ turn: i * turnNum + 'turn', lineTurn: i * turnNum + turnNum / 2 + 'turn', award: awardsConfig[i].img, id: awardsConfig[i].id });
        }
        this.setData({
            awardsList: html
        });
    },
    getLottery: function () {
        var that = this
        that.getto()
        var awardIndex = Math.random() * 6 >>> 0;  //获奖项
       
        // 获取奖品配置
        var awardsConfig = that.data.awardsConfig,
        runNum = 8
        if (awardIndex < 2) awardsConfig.chance = false
        console.log(awardIndex)

        // 旋转抽奖
        var runDegs = that.data.runDegs || 0
        that.data.runDegs = that.data.runDegs + (360 - that.data.runDegs % 360) + (360 * runNum - awardIndex * (360 / 6))

        var animationRun = wx.createAnimation({
            duration: 4000,
            timingFunction: 'ease'
        })
        that.animationRun = animationRun
        animationRun.rotate(that.data.runDegs).step()
        that.setData({
            animationData: animationRun.export()
        })   

        // 中奖提示
        setTimeout(function () { 
            if (awardsConfig.awards[awardIndex].title == "莫灰心"){
                that.setData({
                    awardResult: "很遗憾，你没有中奖。",
                    showTips: true
                })
            }else{
                that.setData({
                    awardResult: '恭喜获得' + (awardsConfig.awards[awardIndex].title),
                    showTips: true
                })
            }
        }, 4000);
    },
    closeLayer: function(){
        this.setData({
            showTips: false
        })
    }
})